// 导入必要的包和类
package com.example.emos.wx.config.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

// 配置类注解，表示该类是一个Spring配置类
@Configuration
public class ShiroConfig {

    // 配置SecurityManager bean，注入OAuth2Realm实例
    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Realm realm){
        // 创建DefaultWebSecurityManager实例
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置领域（Realm）为OAuth2Realm
        securityManager.setRealm(realm);
        // 设置RememberMeManager为null，因为在此示例中不使用RememberMe功能
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    // 配置ShiroFilterFactoryBean bean，注入SecurityManager和自定义的OAuth2Filter
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager, OAuth2Filter filter){
        // 创建ShiroFilterFactoryBean实例
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        // 设置SecurityManager
        shiroFilter.setSecurityManager(securityManager);

        // 配置自定义的Filter
        Map<String, Filter> map = new HashMap<>();
        map.put("oauth2", filter);
        shiroFilter.setFilters(map);

        // 配置URL过滤规则
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/sys/login", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/captcha.jpg", "anon");
        filterMap.put("/user/register", "anon");
        filterMap.put("/user/login", "anon");
        filterMap.put("/test/**", "anon");
        filterMap.put("/meeting/recieveNotify", "anon");
        filterMap.put("/**", "oauth2");

        // 设置过滤规则到ShiroFilterFactoryBean
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    // 配置LifecycleBeanPostProcessor bean，用于在Spring容器中管理Shiro的生命周期
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    // 配置AuthorizationAttributeSourceAdvisor bean，用于启用Shiro注解支持
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        // 设置SecurityManager
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
